<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="35%"
      :modal="false"
      :top="'15%'"
      @close="handleDialogClose"
    >
      <el-form
        ref="forgetPasswordForm"
        :model="forgetPasswordForm"
        label-width="80px"
      >
        <el-form-item label="手机号">
          <span>{{ forgetPasswordForm.phonenumber }}</span>
        </el-form-item>
        <el-form-item label="验证码" prop="verificationCode">
          <el-input v-model="forgetPasswordForm.verificationCode"></el-input>
          <el-button
            :disabled="countdown > 0"
            @click="sendVerificationCode"
            style="margin-top: 10px"
          >
            {{ countdown > 0 ? `${countdown}秒后重新发送` : "发送验证码" }}
          </el-button>
        </el-form-item>
        <!-- 其他相关字段和操作 -->
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handleDialogClose">取消</el-button>
        <el-button type="primary" @click="submitForgetPasswordForm"
          >确定</el-button
        >
      </span>
    </el-dialog>
    <UpdatePassWordDialog
      ref="UpdatePassWordDialog"
      :updateById="true"
    ></UpdatePassWordDialog>
    <BindPhoneNumberDialog ref="BindPhoneNumberDialog"></BindPhoneNumberDialog>
  </div>
</template>

<script>
import UpdatePassWordDialog from "@/components/common/UpdatePassWordDialog.vue";

import BindPhoneNumberDialog from "../bindphonenumber/BindPhoneNumberDialog.vue";

export default {
  props: ["isUpdate"],
  components: {
    UpdatePassWordDialog,
    BindPhoneNumberDialog,
  },
  data() {
    return {
      dialogVisible: false,
      forgetPasswordForm: {
        phonenumber: "",
        verificationCode: "",
        // 其他相关字段
      },
      countdown: 0,
    };
  },
  computed: {
    title() {
      if (this.isUpdate) {
        return "修改手机号";
      }
      return "忘记密码";
    },
  },

  methods: {
    //获取手机号并显示对话框
    showForgetPasswordDialog() {
      // Make a request to obtain the phone number and update forgetPasswordForm
      this.$request.getHttp({
        url: "/user/getPhoneNumberByUserId", // Replace with your actual API endpoint
        params: {
          userId: localStorage.getItem("userId"),
        },
        success: (data) => {
          this.forgetPasswordForm.phonenumber = data.phoneNumber;
          this.dialogVisible = true;
        },
        error: (error) => {
          console.error("Error fetching phone number", error);
        },
      });
    },
    handleDialogClose() {
      this.dialogVisible = false;
      // 清空表单数据
      this.$refs.forgetPasswordForm.resetFields();
      // 重置倒计时
      this.countdown = 0;
    },
    //发送验证码
    sendVerificationCode() {
      // 通过用户的id得到手机号发送验证码
      this.$request.getHttp({
        url: "/sms/sendSmsByUserId",
        params: {
          userId: localStorage.getItem("userId"),
          prefix: this.isUpdate ? "update" : "forgot",
        },
        success: (data) => {
          console.log(data);
          this.$message({
            message: "验证码已发送",
          });

          // 开始倒计时
          this.countdown = 60;
          const timer = setInterval(() => {
            if (this.countdown > 0) {
              this.countdown--;
            } else {
              clearInterval(timer);
            }
          }, 1000);
        },
        error: (error) => {},
      });
    },
    submitForgetPasswordForm() {
      //验证验证码格式
      const verifyCode = /^\d{6}$/;
      if (!verifyCode.test(this.forgetPasswordForm.verificationCode)) {
        this.$message({
          message: "验证码错误，请输入6位数字验证码",
          type: "error",
        });
        return;
      }
      //验证验证码合法性
      this.$request.getHttp({
        url: "/verificationcode/checkCodeByUserIdWithPrefix",
        params: {
          userId: localStorage.getItem("userId"),
          prefix: this.isUpdate ? "update" : "forgot",
          verificationCode: this.forgetPasswordForm.verificationCode,
        },
        success: (data) => {
          console.log(data);
          if (data.code == 500) {
            this.$message({
              message: data.msg,
              type: "error",
            });
            return;
          }
          if (data.code == 200) {
            this.dialogVisible = false;
            if (this.isUpdate) {
              //更新手机号
              this.$refs.BindPhoneNumberDialog.showBindPhoneDialog();
            } else {
              //更新密码
              this.$refs.UpdatePassWordDialog.showChangePasswordDialog();
            }
          }
        },
        error: (error) => {},
      });
    },
  },
};
</script>

<style scoped>
</style>
